package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.IncomeCertificate;
import com.amsp.oa.oademo.mapper.IncomeCertificateMapper;
import com.amsp.oa.oademo.service.IncomeCertificateService;
import com.amsp.oa.oademo.vo.IncomeCertificateVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 收入证明Service实现类
 */
@Service
public class IncomeCertificateServiceImpl extends ServiceImpl<IncomeCertificateMapper, IncomeCertificate>
        implements IncomeCertificateService {

    @Override
    public Page<IncomeCertificateVO> getList(Page<IncomeCertificate> page, String name, String department) {
        // 构建查询条件
        LambdaQueryWrapper<IncomeCertificate> queryWrapper = new LambdaQueryWrapper<>();

        // 如果提供了姓名，添加姓名查询条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like(IncomeCertificate::getName, name);
        }

        // 如果提供了部门，添加部门查询条件
        if (!StringUtils.isEmpty(department)) {
            queryWrapper.like(IncomeCertificate::getDepartment, department);
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(IncomeCertificate::getCreateTime);

        // 执行分页查询
        Page<IncomeCertificate> resultPage = this.page(page, queryWrapper);

        // 转换为VO对象
        Page<IncomeCertificateVO> voPage = new Page<>();
        BeanUtils.copyProperties(resultPage, voPage, "records");

        // 转换记录
        List<IncomeCertificateVO> voList = resultPage.getRecords().stream().map(entity -> {
            IncomeCertificateVO vo = new IncomeCertificateVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());

        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public IncomeCertificateVO getDetail(Long id) {
        // 查询实体
        IncomeCertificate entity = this.getById(id);
        if (entity == null) {
            return null;
        }

        // 转换为VO
        IncomeCertificateVO vo = new IncomeCertificateVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(IncomeCertificateVO incomeCertificateVO) {
        // 转换为实体
        IncomeCertificate entity = new IncomeCertificate();
        BeanUtils.copyProperties(incomeCertificateVO, entity);

        // 保存或更新
        return this.saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<IncomeCertificateVO> incomeCertificateVOList) {
        if (incomeCertificateVOList == null || incomeCertificateVOList.isEmpty()) {
            return false;
        }

        // 转换为实体列表
        List<IncomeCertificate> entityList = incomeCertificateVOList.stream().map(vo -> {
            IncomeCertificate entity = new IncomeCertificate();
            BeanUtils.copyProperties(vo, entity);
            return entity;
        }).collect(Collectors.toList());

        // 批量保存
        return this.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return this.removeByIds(ids);
    }
}